<template>
  <ElPopover :width="256" :visible="visible">
    <template #reference>
      <ElButton :icon="targetIcon" @click="switchShow(true)" @blur="switchShow(false)" style="min-width:130px">
        {{ targetIcon }}
      </ElButton>
    </template>
    <div class="el-icon-picker">
      <component v-for="icon in Object.keys(ElIcon)" :key="icon"
        :class="[icon, 'icon', { 'icon-active': icon == targetIcon }]" :is="icon" @click="selectIcon(icon)" />
    </div>
  </ElPopover>
</template>

<script setup lang="ts">

import * as ElIcon from '@element-plus/icons-vue'
import { ElButton } from 'element-plus'
import { ref } from 'vue'

const targetIcon = defineModel<string>()
const visible = ref<boolean>(false)

/**
 * 显示选择框
 * @param show true：显示；false：隐藏
 */
const switchShow = (show: boolean) => {
  visible.value = show
}

/**
 * 选择一个icon后调用
 * @param icon 
 */
const selectIcon = (icon: string) => {
  targetIcon.value = icon
  visible.value = false
}
</script>

<style scoped>
.el-icon-picker {
  height: 256px;
  overflow-y: scroll;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
}

.icon {
  display: inline-block;
  width: 24px;
  height: 24px;
  color: var(--el-text-color-regular);
  font-size: 20px;
  border-radius: 4px;
  cursor: pointer;
  text-align: center;
  line-height: 45px;
  margin: 5px;
}

.icon:hover {
  color: var(--el-color-primary);
}

.icon-active {
  color: var(--el-color-primary);
}
</style>
